const app = getApp();
Component({
    properties: {

    },
    data: {
        defaultOptions: {
            ar: '1:1',
            iw: 10,
            s: 100,
            q: 10,
            c: 0,
            no: '',
            seed: '',
            niji: '',
            style: '',
            v: '',
            tile: 0,
            image: '',
            words: []
        },
        options: {},
        imageList: []
    },
    lifetimes: {
        attached() {
            this.setData({
                options: JSON.parse(JSON.stringify(this.data.defaultOptions))
            })
        }
    },
    methods: {
        onRadioChange(e) {
            let name = e.detail.name
            let value = e.detail.value
            let options = this.data.options
            options[name] = value
            this.setData({
                options: options
            })
        },
        onSlideChange(e) {
            const name = e.currentTarget.dataset.name
            const value = e.detail.value
            let options = this.data.options
            options[name] = value
            this.setData({
                options: options
            })
        },
        showHelp(e) {
            const text = e.currentTarget.dataset.text
            if (text) {
                app.util.message(text)
            }
        },
        changeSize(e) {
            const value = e.currentTarget.dataset.value
            this.setData({
                'options.ar': value
            })
        },
        // 删除图片
        uploadDelete(e) {
            this.setData({
                imageList: [],
                'options.image': ''
            })
        },
        // 上传图片
        uploadAfterRead(e) {
            let file = e.detail
            wx.showLoading({
                title: '正在上传'
            });
            app.util.upload({
                url: '/upload/image',
                filePath: file.tempFilePaths[0]
            }).then((res) => {
                this.setData({
                    imageList: [{
                        url: res.data.path
                    }],
                    'options.image': res.data.path
                })
                wx.hideLoading();
            }).catch(err => {
                app.util.message('上传失败')
                this.setData({
                    imageList: [],
                    'options.image': ''
                })
            })
        },
        getDrawOptions() {
            const options = JSON.parse(JSON.stringify(this.data.options))
            options.words = this.selectComponent('#prompt').getWordsPicked()
            if (options.iw) {
                options.iw = options.iw / 10;
            }
            if (options.q) {
                options.q = options.q / 10
            }
            return options
        },
        clearDrawOptions() {
            this.setData({
                'options.image': '',
                'options.no': '',
                'options.seed': '',
            })
            this.selectComponent('#prompt').clearPicked()
        },
        setDrawOptions(options) {
            if (options.iw) {
                options.iw = options.iw * 10
            }
            if (options.q) {
                options.q = options.q * 10
            }
            options = Object.assign(this.data.defaultOptions, options)
            this.setData({
                options: JSON.parse(JSON.stringify(options))
            })
            if (options.image) {
                this.setData({
                    imageList: [{
                        url: options.image
                    }]
                })
            }
            if (options.words) {
                this.selectComponent('#prompt').setWords({detail: options.words})
            }
        },
    }
})